#include <SDL_mouse.h>
#include "../Core/Types.h"
#include "Mouse.h"


struct MouseState
{
	MouseState() : x(0), y(0), dx(0), dy(0), buttons(0) { }
	int x;
	int y;
	int dx;
	int dy;
	uint buttons;
};

static MouseState curr, prev;


bool Mouse::IsButtonDown(Button button)
{
	uint mask = (1 << button);
	return (curr.buttons & mask) == mask;
}

bool Mouse::IsButtonUp(Button button)
{
	uint mask = (1 << button);
	return (curr.buttons & mask) == 0;
}

bool Mouse::WasButtonPressed(Button button)
{
	uint mask = (1 << button);
	return ((curr.buttons & mask) == mask) && ((prev.buttons & mask) == 0);
}

bool Mouse::WasButtonReleased(Button button)
{
	uint mask = (1 << button);
	return ((curr.buttons & mask) == 0) && ((prev.buttons & mask) == mask);
}

void Mouse::GetPosition(int& x, int& y)
{
	x = curr.x;
	y = curr.y;
}

void Mouse::GetMotion(int& x, int& y)
{
	x = curr.dx;
	y = curr.dy;
}

void Mouse::Update()
{
	prev = curr;
	SDL_GetMouseState(&curr.x, &curr.y);
	curr.buttons = SDL_GetRelativeMouseState(&curr.dx, &curr.dy);
}
